#!/bin/sh
#
# webssh -- login the docker lab via web ssh
#

TOP_DIR=$(cd $(dirname $0)/../../ && pwd)
. $TOP_DIR/tools/docker/config $* >/dev/null

do_unlock
get_var WEB_BROWSER chromium-browser
get_var HOST localhost
get_var UNIX_USER ubuntu
get_var UNIX_PWD ubuntu
get_var VNC_IP $(docker inspect --format '{{ .NetworkSettings.IPAddress }}' $CONTAINER_NAME)

do_lock

which $WEB_BROWSER >/dev/null 2>&1
if [ $? -eq 1 ]; then
    echo "LOG: No $WEB_BROWSER found, use firefox instead."
    WEB_BROWSER=firefox
fi

WEBSSH_URL="$WEB_HTTP://${HOST}:${HOST_WEBSSH_PORT}?ssh=ssh://$UNIX_USER:$UNIX_PWD@$VNC_IP:$CONTAINER_SSH_PORT"
WEBSSH_CMD="$WEB_BROWSER $WEBSSH_URL"

echo "LOG: Please login with 'ip=$VNC_IP, port=$CONTAINER_SSH_PORT, user=$UNIX_USER, password=$UNIX_PWD' and enter workdir: $LAB_WORKDIR\n"

($WEBSSH_CMD >/dev/null 2>&1 &) >/dev/null 2>&1

echo "LOG: Or access via web browser:\n  $WEBSSH_URL"
